import {coverGlobal} from '@swda/utils'
//-----------------------------------------------------------------------------
// Window_MapName
//
// The window for displaying the map name on the map screen.

function Window_MapName() {
    this.initialize.apply(this, arguments);
}

Window_MapName.prototype = Object.create(Window_Base.prototype);
Window_MapName.prototype.constructor = Window_MapName;

Window_MapName.prototype.initialize = function () {

    var width = this.windowWidth();
    var height = this.windowHeight();
    var x = (Graphics.boxWidth - width) / 2;
    var y = 180;

    Window_Base.prototype.initialize.call(this, x, y, width, height);

    this.opacity = 0;
    this.contentsOpacity = 0;
    this._showCount = 0;
    this.refresh();
};

Window_MapName.prototype.windowWidth = function () {
    return 384 + this.standardPadding() * 2;
};

Window_MapName.prototype.windowHeight = function () {
    return 96 + this.standardPadding() * 2;
};

Window_MapName.prototype.update = function () {
    Window_Base.prototype.update.call(this);
    if (this._showCount > 0 && $gameMap.isNameDisplayEnabled()) {
        this.updateFadeIn();
        this._showCount--;
    } else {
        this.updateFadeOut();
    }
};

Window_MapName.prototype.updateFadeIn = function () {
    this.contentsOpacity += 16;
};

Window_MapName.prototype.updateFadeOut = function () {
    this.contentsOpacity -= 16;
};

Window_MapName.prototype.open = function () {
    this.refresh();
    this._showCount = 75;
};

Window_MapName.prototype.close = function () {
    this._showCount = 0;
};
// 显示文字
Window_MapName.prototype.refresh = function () {
    this.contents.clear();
    if ($gameMap.displayName()) {
        var width = this.contentsWidth();
        
        var y = (this.contentsHeight() - this.contents.fontSize - this.textPadding() * 2) / 2;
        this.drawBackground(0, 0, width, this.lineHeight());
        var mapNameLength = $gameMap.displayName().length;
        if (mapNameLength>1){
            var interv = (width - this.standardPadding()*2  - this.textPadding()*4- this.contents.fontSize * mapNameLength) / (mapNameLength - 1) + this.contents.fontSize;
            for (var i=0; i<mapNameLength; i++){
                this.drawText($gameMap.displayName()[i], this.standardPadding() + this.textPadding()*2 + i*interv, y, this.contents.fontSize);
            }
        }else{
            this.drawText($gameMap.displayName(), (width - this.contents.fontSize)/2 , y, this.contents.fontSize);
        }
    }
};

Window_MapName.prototype.drawBackground = function () {

    var bitmap = ImageManager.loadBitmap('img/system/UI/', 'MapName');
    var pw = bitmap.width;
    var ph = bitmap.height;
    this.contents.blt(bitmap, 0, 0, pw, ph, 0, 0);
};
// 存入存档的地图名：有“显示名称”的用显示名，否则不断向父级查找。
Game_Map.prototype.saveName = function() {
    let saveName = $dataMap.displayName;
    while (saveName === ''){
        let mapId = this._mapId
        let parentId = $dataMapInfos[mapId].parentId
        if(parentId > 0)
            saveName = $dataMapInfos[parentId].name;
        else
            saveName ='？？？'
    }
    return saveName;
};


coverGlobal('Window_MapName',Window_MapName) // reset window_mapname